<?php
/**
 * Created by PhpStorm.
 * User: Lzhy ysjywz@gmail.com
 * Date: 2018/5/19
 * Time: 下午3:01
 */
namespace Core\common;

use Core\Traits\Template;

class Controller
{
    use Template;

    public $controller;
    public $action;
    public $static;
    
    function __construct()
    {
        $do = Request::params('do','index');
        $op = Request::params(Assist::globalVal('yWe7.action_name'),'index');
        $static = MODULE_URL.Assist::globalVal('yWe7.static').'/';
        $this->controller = $do;
        $this->action = $op;
        $this->static = $static;
    }

    /**
     * 渲染视图
     * @param string $view 视图名
     * @param array $data 发送数据
     * @param string $module
     */
    public function display($view = '', $data = [],$module = '')
    {
        if($view && strpos($view,'/') === false){
            $do = $this->controller;
            $view = $do.'/'.$view;
        }
        if(empty($view)){
            $view = $this->controller.'/'.$this->action;
        }
        $data['static'] = $this->static;
        $data['__static__'] = $this->static;
        
        echo $this->fetch($view,$data);
    }

    /**
     * @param $name
     * @return mixed|string
     */
    public static function template($name)
    {
        return template($name,TEMPLATE_INCLUDEPATH);
    }
}